﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using SharePointBrowser.Properties;
using Microsoft.WindowsMobile.PocketOutlook;
using SharePointBrowser.WebsWS;
using System.Net;
using System.Xml;
using SharePointBrowser.SiteDataWS;
using SharePointBrowser.ListsWS;
using SharePointBrowser.Data;
//using PhotoContacts;

namespace SharePointBrowser
{
    public class WebList : SlidingListControl
    {
        private string siteUrl;
        private Stack<string> history = new Stack<string>();

        public event EventHandler RootSiteReached;
        public event EventHandler SubsiteOpened;
        public event EventHandler<ListEventArgs> ListOpened;

        public string SiteUrl
        {
            get { return siteUrl; }
            set { siteUrl = value; }
        }

        public bool AtRootSite
        {
            get { return history.Count == 0; }
        }

        public WebList() { }

        public override void LoadData()
        {
            int width = Width;
            int height = Height;

            System.Threading.Thread asyncLoading = new System.Threading.Thread(delegate()
            {
                Invoke(new UIThreadDelegate(ShowWaitingCursor));
                _IsDataLoading = true;

                internalDataList = new List<ISlidingListItem>();

                int y = 0;

                XmlNode lists = SPAdapter.Instance.GetListCollection(siteUrl);

                foreach (XmlNode list in lists.SelectNodes("sp:List", SPAdapter.Instance.NamespaceManager))
                {
                    if (Configuration.Instance.ShowHiddenLists || !Convert.ToBoolean(list.Attributes["Hidden"].Value))
                    {
                        SPListItem item = new SPListItem(list);
                        item.Location = new Point(0, y);
                        item.Size = new Size(width, 60);
                        item.ItemCount = Convert.ToInt32(list.Attributes["ItemCount"].Value);
                        item.Clicked += (sender, e) =>
                        {
                            if (ListOpened != null)
                            {
                                ListOpened(this, new ListEventArgs() { ListId = ((SPListItem)sender).ListId, Name = ((SPListItem)sender).Name });
                            }
                        };
                        internalDataList.Add(item);
                        y += 60;
                    }
                }

                XmlNode webs = SPAdapter.Instance.GetWebCollection(siteUrl);

                foreach (XmlNode web in webs.SelectNodes("sp:Web", SPAdapter.Instance.NamespaceManager))
                {
                    SPWebItem item = new SPWebItem(web);
                    XmlNode w = SPAdapter.Instance.GetWeb(item.Url);
                    item.Location = new Point(0, y);
                    item.Size = new Size(width, 60);
                    item.Description = w.Attributes["Description"].Value;
                    item.Clicked += (sender, e) =>
                    {
                        history.Push(siteUrl);
                        siteUrl = ((SPItem)sender).Url;
                        LoadData();
                        CustomInvalidate();
                        if (SubsiteOpened != null)
                        {
                            SubsiteOpened(this, null);
                        }
                    };
                    internalDataList.Add(item);
                    y += 60;
                }
                SlidingPanel_Height = Math.Max(y, height);
                SlidingPanel_TranslateY = 0;

                _IsDataLoading = false;
                Invoke(new UIThreadDelegate(HideWaitingCursor));
                Invoke(new UIThreadDelegate(CustomInvalidate));
            });
            asyncLoading.IsBackground = true;
            asyncLoading.Start();
        }

        private void ShowWaitingCursor()
        {
            Cursor.Current = Cursors.WaitCursor;
        }

        private void HideWaitingCursor()
        {
            Cursor.Current = Cursors.Default;
        }

        public void GoBack()
        {
            if (history.Count > 0)
            {
                siteUrl = history.Pop();
                LoadData();
                CustomInvalidate();
                if (history.Count == 0 && RootSiteReached != null)
                {
                    RootSiteReached(this, null);
                }
            }
        }
    }
}
